package objectFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import vo.UserVO;

public class UserDao {

		private ConnectionMaker connectionMaker;
		
		public void setUserDao(ConnectionMaker connectionMaker){
			this.connectionMaker = connectionMaker;
		}
		
		public void add(UserVO user) throws ClassNotFoundException,SQLException{
				Connection c = connectionMaker.makeConnection();
				PreparedStatement psmt = c.prepareStatement("insert into users(id,name,password) values (?,?,?)");
				psmt.setString(1, user.getId());
				psmt.setString(2,user.getName());
				psmt.setString(3,user.getPassword());
				
				psmt.executeUpdate();
				
				psmt.close();
				c.close();
		}
	
		public UserVO get(String id) throws ClassNotFoundException,SQLException{
			UserVO user = new UserVO();
			
			Connection c = connectionMaker.makeConnection();
			PreparedStatement psmt = c.prepareStatement("select * from users where id = ?");
			
			psmt.setString(1, id);
			
			ResultSet rs =  psmt.executeQuery();
			
			while(rs.next()){
				user.setId(rs.getString("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
			}
			
			
			rs.close();
			psmt.close();
			c.close();
			
			return user;	
			
	}
}
